using TMPro;
using UnityEngine;

namespace Assets.Scripts.Entities
{
    internal class FloatingNumber : MonoBehaviour
    {
        public event System.Action Hided;

        [field: SerializeField] private float _floatDuration { get; set; } = 1.5f; // How long the number will float before disappearing
        [field: SerializeField] private float _floatSpeed { get; set; } = 1f; // Speed at which the number floats upwards
        [SerializeField] private AnimationCurve _scaleCurve;

        private float timer = 0f;
        private Vector3 _originalScale;

        public string Text
        {
            get => GetComponent<TMP_Text>().text;
            set => GetComponent<TMP_Text>().text = value;
        }

        public void ResetValues()
        {
            timer = 0;
        }

        public void Set(string text)
        {
            GetComponent<TMP_Text>().text = text;
        }

        public void Set(string text, Color color)
        {
            var txt = GetComponent<TMP_Text>();
            txt.text = text;
            txt.color = color;
        }

        private void Start()
        {
            _originalScale = transform.localScale;
        }

        void Update()
        {
            timer += Time.deltaTime;

            var factor = _scaleCurve.Evaluate(timer / _floatDuration);
            transform.localScale = _originalScale * factor;

            if (timer >= _floatDuration)
            {
                gameObject.SetActive(false);
                Hided?.Invoke();
            }
            else
            {
                transform.position += new Vector3(0f, _floatSpeed * Time.deltaTime, 0f); // Float upwards
            }
        }
    }
}
